﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;
using AGrandVenture;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;

namespace DataEditor
{
    public partial class RaceForm : Form
    {
        private Form1 form;
        private List<Race> races;

        private List<Stat> stats;

        public RaceForm(Form1 form, List<Stat> stats)
        {
            InitializeComponent();
            this.form = form;
            this.stats = stats;

            races = new List<Race>();

            LoadStats();
        }

        private void LoadStats()
        {
            lstStats.Items.Clear();

            foreach (Stat stat in stats)
            {
                lstStats.Items.Add(stat.Name);
            }
        }

        private void CheckModifierAmount()
        {
            if (!string.IsNullOrEmpty(txtModifierAmount.Text) && lstStats.SelectedIndex > -1)
                btnAddModifier.Enabled = true;
            else
                btnAddModifier.Enabled = false;
        }




        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void txtModifierAmount_TextChanged(object sender, EventArgs e)
        {
            CheckModifierAmount();
        }

        private void btnAddModifier_Click(object sender, EventArgs e)
        {
            lstStatMods.Items.Add(lstStats.Items[lstStats.SelectedIndex] + ": " + txtModifierAmount.Text);
            lstStats.Items.Remove(lstStats.SelectedItem);
            txtModifierAmount.Text = "";
        }

        private void btnRemoveModifier_Click(object sender, EventArgs e)
        {
            string item = lstStatMods.Items[lstStatMods.SelectedIndex].ToString();
            lstStats.Items.Add(item.Substring(0, item.IndexOf(":")));
            lstStatMods.Items.Remove(item);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNewRace_Click(object sender, EventArgs e)
        {
            txtRaceName.Text = "";
            txtRaceDescription.Text = "";
            txtModifierAmount.Text = "";
            txtResistanceModifierAmount.Text = "";
            lstStatMods.Items.Clear();
            lstResistanceMods.Items.Clear();

            LoadStats();
        }

        private void btnSaveRace_Click(object sender, EventArgs e)
        {
            try
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;

                foreach (Race race in races)
                {
                    XmlWriter writer = XmlWriter.Create(Application.StartupPath + @"\XML\" + @"\races\" + race.Name + ".xml", settings);

                    IntermediateSerializer.Serialize(writer, race, null);

                    writer.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnAddRace_Click(object sender, EventArgs e)
        {
            Race race = new Race(txtRaceName.Text);
            race.Description = txtRaceDescription.Text;

            races.Add(race);

            cboRaces.Items.Add(race.Name);

            //Add Stat Modifiers
            if (lstStatMods.Items.Count > 0)
            {
                //Create StatMod dictionary if it does not exist
                if (race.StatModifiers == null)
                    race.StatModifiers = new Dictionary<string, int>();

                //Overwrite exisiting StatMods if they exist
                if (race.StatModifiers.Count > 0)
                    race.ClearStatMods();

                string stat, abbr;
                int val;

                for (int i = 0; i < lstStatMods.Items.Count; i++)
                {
                    stat = lstStatMods.Items[i].ToString();
                    val = Convert.ToInt32(stat.Substring(stat.IndexOf(":") + 2)); // the +2 is because of the space between : and the value
                    abbr = GetStatAbbreviation(stat.Substring(0, stat.IndexOf(":")));

                    if (abbr != "")
                        race.AddStatModifier(abbr, val);
                    else
                        MessageBox.Show("Unable to retrieve Stat Abbreviation for " + stat);
                }
            }

            btnNewRace_Click(sender, e);
        }


        private void cboRaces_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboRaces.SelectedIndex > -1)
            {
                Race race = races[cboRaces.SelectedIndex];

                txtRaceName.Text = race.Name;
                txtRaceDescription.Text = race.Description;
            }
            else
            {
                btnNewRace_Click(sender, e);
            }
        }


        private string GetStatAbbreviation(string statName)
        {
            foreach (Stat stat in stats)
                if (stat.Name == statName)
                    return stat.Abbreviation;

            return "";
        }

        private string GetStatNameFromAbbreviation(string statAbbreviation)
        {
            foreach (Stat stat in stats)
                if (stat.Abbreviation == statAbbreviation)
                    return stat.Name;

            return "";
        }

        private void btnLoadRace_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Multiselect = true;

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Race race;

                foreach (string file in dlg.FileNames)
                {
                    race = GlobalFunctions.LoadRace(file);

                    if (race != null)
                    {
                        races.Add(race);

                        cboRaces.Items.Add(race.Name);
                    }
                }
            }
        }
    }
}
